package com.zcb.myrxjavathreecache.tools;

/**
 * Created by Administrator on 2017/2/7.
 */

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * set方法中是timestamp类型
     * new Timestamp(s)
     * @return s
     */
    public Timestamp newStampTime(){
        long s= System.currentTimeMillis();
        return new Timestamp(s);
    }
    /**
     * timeStamp转换为String
     * @return
     */
    public String timeStampToString(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式，不显示毫秒
        Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String str = df.format(now);
        return str;
    }
    /**
     * String 转换为timestamp
     * @return
     */
    public Timestamp StringTotimeStamp(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Timestamp ts = Timestamp.valueOf(time);
        return ts;
    }
    public Date myStringTotimeStamp(String dateString){
        int length=dateString.length();
        if(length>10){
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date today=new Date();
            try {

                today = df.parse("2009-11-11");
                //String ts=df.format(today);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return today;
        }else {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date today=new Date();
            try {
                today = df.parse("2009-11-11");
                //String ts=df.format(today);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return today;
        }

    }
    /**
     * String 转换为timestamp
     * String "2016-5-25" 转换为 Timestamp
     * @return
     */
    public Timestamp StringTotimeStamp(String dateString){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer ds=new StringBuffer(dateString);
        if(dateString.length()>10){
            Timestamp ts = Timestamp.valueOf(dateString);
            return ts;
        }else{
            ds.append(" 00:00:00");
            Timestamp ts = Timestamp.valueOf(ds.toString());
            return ts;
        }
    }
    /**
     * 获取当前时间字符串
     * eg:2016-10-11 16:57:52
     **/
    public static String formateDateString(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
        String dateString=format.format(new Date());
        return dateString;
    }
    /**
     * 指定时间和当前时间比较大小
     */
    public boolean compareTime(String t1){
        if(t1.length()<=10){
            t1=t1+" 23:59:59";
        }
        Date currentTime = new Date();// 当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowtime = formatter.format(currentTime);

        Calendar now = Calendar.getInstance();
        Calendar c1 = Calendar.getInstance();
        //String t1 = "2016-02-29 00:30:00";
        try {
            now.setTime(formatter.parse(nowtime));
            c1.setTime(formatter.parse(t1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result1 = now.compareTo(c1);// 比开始时间小，未开始
        System.out.println("结果:"+result1);
        if(result1>0){
            return false;
        }else{
            return true;
        }
    }



}
